package com.guanlixitong.dao.impl;

import com.guanlixitong.dao.IStudentDao;
import com.guanlixitong.domain.Student;
import com.guanlixitong.wenjian.util.ExportExcel;
import com.guanlixitong.wenjian.util.ImportExcel;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class OtherStudentDaoImpl implements IStudentDao {
    private static ArrayList<Student> lists = new ArrayList<>();

    //静态代码块读学生
    static {

        ObjectInputStream ois=null;
        try {
           ois = new ObjectInputStream(new FileInputStream("D:\\students.txt"));
            lists = (ArrayList<Student>) ois.readObject();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static String username;

    //获取用户名
    @Override
    public void setUsername(String username) {
        OtherStudentDaoImpl.username = username;
    }

    //更新数据
    @Override
    public void tst() {

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("D:\\students.txt"));
            oos.writeObject(lists);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //获取索引
    @Override
    public int getIndex(String sid) {
        int index = -1;
        for (int i = 0; i < lists.size(); i++) {
            if (sid.equals(lists.get(i).getId())) {
                index = i;
            }
        }
        return index;
    }

    //增
    @Override
    public void addstudent(Student student) throws IOException {
        lists.add(student);


        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt", StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "添加学生" + student);
        bw.write("\r\n");
        bw.flush();
        bw.close();

    }

    //删
    @Override
    public void deletestudent(String sid) throws IOException {
        int index = getIndex(sid);
        Student remove = lists.remove(index);

        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "删除学生" + remove);
        bw.write("\r\n");
        bw.flush();
        bw.close();

    }

    //改
    @Override
    public void updatestudent(String sid, Student student) throws IOException {
        int index = getIndex(sid);
        Student student1 = lists.set(index, student);
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "将学生" + student1 + "修改为：" + student);
        bw.write("\r\n");
        bw.flush();
        bw.close();

    }

    //导出数据
    @Override
    public void excel() throws Exception {

        LocalDateTime start = LocalDateTime.now();
        HSSFWorkbook weebBook = new HSSFWorkbook();
        String[] arr = {"序列", "学号", "姓名", "性别", "年龄", "生日", "组号","科目", "手机号", "微信号"};
        ArrayList<Object[]> list = new ArrayList<>();

        HSSFSheet sheet = weebBook.createSheet("页签" + 1);
        for (int i = 0; i < lists.size(); i++) {
            list.add(new String[]{String.valueOf(i + 1), lists.get(i).getId(), lists.get(i).getName(), lists.get(i).getSex(), String.valueOf(lists.get(i).getAge()), String.valueOf(lists.get(i).getBirthday()), String.valueOf(lists.get(i).getGroup()),lists.get(i).getSubject(), lists.get(i).getPhone(), String.valueOf(lists.get(i).getList())});
        }
        ExportExcel excel = new ExportExcel("学生信息管理", arr, list);
        excel.export(weebBook, sheet, 1);
        LocalDateTime end = LocalDateTime.now();
        System.out.println("导出数据成功，耗时"+Duration.between(start, end).toMillis() + "ms");
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "导出数据");
        bw.write("\r\n");
        bw.flush();
        bw.close();
    }

    //导入数据
    @Override
    public void importExcel(String s) throws Exception {

        LocalDateTime start = LocalDateTime.now();
        List<String[]> strings = ImportExcel.readExcel(new File(s));
        for (String[] string : strings) {
            System.out.println(Arrays.toString(string));
        }
        LocalDateTime end = LocalDateTime.now();
        System.out.println("导入数据成功，耗时"+Duration.between(start, end).toMillis() + "ms");
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "导入数据");
        bw.write("\r\n");
        bw.flush();
        bw.close();

    }

    @Override
    //返回学生列表
    public ArrayList<Student> findAllstudent() throws IOException {
        return lists;
    }

    //读日志
    @Override
    public void readRizhi() throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader("D:\\rizhi.txt",StandardCharsets.UTF_8));
        String sLine = null;
        while ((sLine = reader.readLine()) != null) {
            System.out.println(sLine);
        }

        reader.close();
    }

    //退出
    @Override
    public void exit() throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\rizhi.txt",StandardCharsets.UTF_8,true));
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String f2 = pattern.format(now);
        bw.write(username + " " + f2 + " " +  "退出");
        bw.write("\r\n");
        bw.flush();
        bw.close();
    }
}

